<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片试图区域 -->
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="7"
          ><el-input
            placeholder="请输入内容"
            class="input-with-select"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
            ></el-button> </el-input
        ></el-col>

        <el-col :span="4">
          <el-button type="primary">添加用户</el-button>
        </el-col>
      </el-row>

       <!-- 用户列表 -->
    <el-table border stripe="" :data="userList" style="width: 100%">
         <el-table-column type="index"></el-table-column>
         <el-table-column  prop="username" label="姓名" width="180"> </el-table-column>
         <el-table-column  prop="email" label="邮箱"> </el-table-column>
         <el-table-column  prop="mobile" label="电话"> </el-table-column>
         <el-table-column  prop="role_name" label="角色"> </el-table-column>
         <el-table-column  label="状态"> 
            <template slot-scope="scope">
               <el-switch  v-model="scope.row.mg_state" @change="userStateChanged(scope.row)" > </el-switch>
            </template>
         </el-table-column>
         <el-table-column   label="操作" width="180px"> 
             <template >
               <el-tooltip class="item" effect="dark" content="编辑" placement="top">
                 <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
                 </el-tooltip>
                 <el-tooltip class="item" effect="dark" content="删除" placement="top">
                 <el-button type="danger" icon="el-icon-delete" size="mini"></el-button>
                 </el-tooltip>
                  <el-tooltip class="item" effect="dark" content="分配角色" placement="top">
                 <el-button type="warning" icon="el-icon-setting" size="mini"></el-button>
                 </el-tooltip>
             </template>
         </el-table-column>
    </el-table>
      <el-pagination  @size-change="handleSizeChange" 
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum" :page-sizes="[1,2, 5, 10]"
           :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper"
          :total="total">
    </el-pagination>

    </el-card>
   
  </div>
</template>

<script>
export default {
  name: 'Vue2shopUser',

  data() {
    return {
        //获取用户列表的参数
        queryInfo:{
            query:'',
            pagenum:1,
            pagesize:2,
        },
        userList:[],
        total:0
    }
  },
  created() {
      this.getUserList()
  },
  mounted() {},

  methods: {
    async getUserList() {
        const{data:res}= await this.$http.get('users',{ 
            params:this.queryInfo
        })
        // console.log(res)
        if(res.meta.status !== 200 ) return this.$message.error("获取用户失败！")
        this.userList=res.data.users
        this.total=res.data.total
      },
      //改变信息显示条数
      handleSizeChange(newSize){
        // console.log(newSize)
        this.queryInfo.pagesize=newSize
        this.getUserList()
      },
      // 改变当前页码数
      handleCurrentChange(newPage){
        // console.log(newPage)
        this.queryInfo.pagenum=newPage
        this.getUserList()
      },
    async  userStateChanged(row){
        console.log(row.mg_state)
      const{data:res}= await this.$http.put('users/'+row.id+'/state/'+row.mg_state)
      if(res.meta.status !==200){
        row.mg_state=!row.mg_state
        // console.log(res)
        return this.$message.error("更改状态失败！")
      }
      console.log(res)
     }
  },
}
</script>

<style lang="scss" scoped>
</style>